#include "ConfigWidget.h"
#include <QtGui/QCheckBox>



ConfigWidget::ConfigWidget(QWidget* parent):QWidget(parent)
{
	// init layout
	m_layout = new QVBoxLayout();
	m_layout->setContentsMargins(1, 1, 1, 1);

	QVBoxLayout* layout = new QVBoxLayout();
	layout->setSizeConstraint(QLayout::SetNoConstraint);
	layout->setContentsMargins(1, 1, 1, 1);
	layout->addLayout(m_layout,100);

	// add a textbrowser that will display text
	m_textBrowser = new QTextBrowser();
	m_textBrowser->setBaseSize(QSize(300,300));
	m_textBrowser->setSizePolicy(QSizePolicy(QSizePolicy::Fixed,QSizePolicy::MinimumExpanding));
	layout->addWidget(m_textBrowser);
	QSpacerItem* spacer = new QSpacerItem(250,100, QSizePolicy::Expanding, QSizePolicy::Expanding);
	layout->addSpacerItem(spacer);
	setLayout(layout);
}

ConfigWidget::~ConfigWidget()
{
	m_NodeList.clear();
	delete(m_layout);
	delete(m_textBrowser);
}

void ConfigWidget::atNodeAsCheckBox(osg::Node* node, QString name )
{
	// Add a checkbox that will control the rendering state of the node
	QCheckBox* checkBox = new QCheckBox(name);
	checkBox->setChecked(true);
	int index = m_layout->count();
	m_layout->addWidget(checkBox,index);
	connect(checkBox,SIGNAL(stateChanged(int)),this,SLOT(checkBoxStateChanged(int)));
	m_NodeList.push_back(node);
}

void ConfigWidget::checkBoxStateChanged(int checkState)
{
	// iterate over all checkboxes
	int index = m_layout->count();
	for(int i = 0; i < index; i++)
	{
		QLayoutItem* item = m_layout->itemAt(i);
		if(item != NULL)
		{
			QWidget* widget = item->widget();
			if(widget!=NULL)
			{
				// check the state
				QCheckBox* currentBox = dynamic_cast<QCheckBox*>(widget);
				bool state = currentBox->isChecked();
				osg::ref_ptr<osg::Node> currentNode = m_NodeList.at(i);
				// and apply it to the node
				currentNode->setNodeMask(int(state));
			}
		}
	}
}

void ConfigWidget::displayText(QString text)
{
	// clear old text 
	m_textBrowser->clear();
	// and set new text
	m_textBrowser->setText(text);
}
